import request from '../../util/request';

/**
 * 工作空间模块接口封装
 */
export const workspacesApi = {
    // 获取所有工作空间列表
    getWorkspaces: () => request.get('v1/workspaces'),

    // 创建新的工作空间
    createWorkspace: (data) =>
        request.post('v1/workspace/new', data),

    // 查看特定工作空间信息
    getWorkspaceInfo: (slug) => request.get(`v1/workspace/${slug}`),

    // 删除工作空间
    deleteWorkspace: (slug) => request.delete(`v1/workspace/${slug}`),

    // 更新工作空间设置
    updateWorkspace: (slug, { name, similarityThreshold, openAiTemp, openAiHistory, openAiPrompt, chatMode }) =>
        request.post(`v1/workspace/${slug}/update`, {
            name, similarityThreshold, openAiTemp, openAiHistory, openAiPrompt, chatMode,
        }),

    // 获取工作空间的聊天记录
    getWorkspaceChats: (slug) => request.get(`v1/workspace/${slug}/chats`),

    // 创建子线程
    createThread: (slug, { name }) =>
        request.post(`v1/workspace/${slug}/chat`, {
            data: { name }
        })
};

/**
 * 线程
 */
export const threadsApi = {
    // 创建子线程
    createThread: (workspaceSlug, data) =>
        request.post(`v1/workspace/${workspaceSlug}/thread/new`, data),

    // 更新子线程名称
    updateThread: (workspaceSlug, threadSlug, name) =>
        request.post(`v1/workspace/${workspaceSlug}/thread/${threadSlug}/update`, {
            name
        }),

    // 删除子线程
    deleteThread: (workspaceSlug, threadSlug) =>
        request.delete(`v1/workspace/${workspaceSlug}/thread/${threadSlug}`),
    // 获取聊天记录
    getThreadChats: (workspaceSlug, threadSlug) =>
        request.get(`v1/workspace/${workspaceSlug}/thread/${threadSlug}/chats`),
    // 流式发送消息
    streamChat: (workspaceSlug, threadSlug, message, mode) =>
        request.post(`/v1/workspace/${workspaceSlug}/thread/${threadSlug}/stream-chat`, {
            message,
            mode
        })
};

/**
 * 管理模块接口封装
 */
export const adminApi = {
    // 获取系统是否处于多用户模式
    isMultiUserMode: () => request.get('v1/admin/is-multi-user-mode'),

    // 获取用户列表
    getUsers: () => request.get('v1/admin/users'),

    // 创建新用户
    createUser: ({ username, email, role }) =>
        request.post('v1/admin/users/new', { data: { username, email, role } }),

    // 更新用户信息
    updateUser: (id, { username, email, role }) =>
        request.put(`v1/admin/users/${id}`, { data: { username, email, role } }),

    // 删除用户
    deleteUser: (id) => request.delete(`v1/admin/users/${id}`),

    // 管理工作空间的用户
    manageWorkspaceUsers: (workspaceId, { userIds }) =>
        request.put(`v1/admin/workspaces/${workspaceId}/update-users`, { data: { userIds } }),

    // 获取工作空间的用户列表
    getWorkspaceUsers: (workspaceId) => request.get(`v1/admin/workspaces/${workspaceId}/users`),

    // 管理邀请
    manageInvite: ({ email, role, workspaceId }) =>
        request.post('v1/admin/invite/new', { data: { email, role, workspaceId } }),

    // 获取邀请列表
    getInvites: () => request.get('v1/admin/invites'),

    // 更新系统偏好设置
    updatePreferences: ({ theme, language }) =>
        request.put('v1/admin/preferences', { data: { theme, language } })
};

/**
 * 文档模块接口封装
 */
export const documentApi = {
    // 上传文档
    uploadDocument: (formData) =>
        request.post('v1/document/upload', formData, {
            headers: { 'Content-Type': 'multipart/form-data' }
        }),

    // 上传文档到指定文件夹
    uploadDocumentToFolder: (folderName, formData) =>
        request.post(`v1/document/upload/${folderName}`, formData, {
            headers: { 'Content-Type': 'multipart/form-data' }
        }),

    // 上传链接
    uploadDocumentByLink: ({ url, folderName }) =>
        request.post('v1/document/upload-link', { url, folderName }),

    // 上传原始文本
    uploadRawText: ({ text, folderName }) =>
        request.post('v1/document/raw-text', { text, folderName }),

    // 获取全部文档
    getDocuments: () => request.get('v1/documents'),

    // 获取指定文件夹下文档
    getDocumentsByFolder: (folderName) => request.get(`v1/documents/folder/${folderName}`),

    // 支持的文件类型
    getAcceptedFileTypes: () => request.get('v1/document/accepted-file-types'),

    // 获取元数据 schema
    getDocumentMetadataSchema: () => request.get('v1/document/metadata-schema'),

    // 获取单个文档详情
    getDocumentByName: (docName) => request.get(`v1/document/${docName}`),

    // 创建文件夹
    createFolder: ({ folderName }) =>
        request.post('v1/document/create-folder', { name: folderName }),

    // 删除文件夹
    removeFolder: ({ folderName }) =>
        request.delete('v1/document/remove-folder', { data: { name: folderName } }),

    // 移动文件
    moveFiles: ({ files }) =>
        request.post('v1/document/move-files', { files }),

    // 删除文档
    deleteDocument: ({ name }) =>
        request.delete(`v1/document/${name}`),

    // 嵌入文档到向量库
    updateEmbeddings: (slug, { adds = [], deletes = [] }) =>
        request.post(`v1/workspace/${slug}/update-embeddings`, { adds, deletes }),      

    // 解绑文档
    updatePin: (workspaceSlug, { docPaths }) =>
        request.post(`v1/workspace/${workspaceSlug}/update-pin`, { docPaths })
};


export const embedApi = {
    // 获取所有嵌入
    getEmbeds: () => request.get('v1/embed'),

    // 获取指定嵌入的聊天记录
    getEmbedChats: (embedUuid) => request.get(`v1/embed/${embedUuid}/chats`),

    // 获取指定 session 聊天内容
    getEmbedSession: (embedUuid, sessionUuid) =>
        request.get(`v1/embed/${embedUuid}/chats/${sessionUuid}`),

    // 创建新的嵌入
    createEmbed: (data) => request.post('v1/embed/new', { data }),

    // 更新嵌入配置
    updateEmbed: (embedUuid, data) =>
        request.post(`v1/embed/${embedUuid}`, { data }),

    // 删除嵌入配置
    deleteEmbed: (embedUuid) =>
        request.delete(`v1/embed/${embedUuid}`)
};
